Slider.js ➔ ???   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 22
rs 9.2
c 1
b 0
f 0
cc 2
nc 2
nop 1
1
class Slider {
2
3
    constructor(element) {
4
        this.options = {
5
            infinite: 1,
6
            classNameFrame: 'slider-block__frame',
7
            classNameSlideContainer: 'slider-block__slides',
8
            enableMouseEvents: true,
9
            classNamePrevCtrl: 'prev',
10
            classNameNextCtrl: 'right',
11
            slideSpeed: 300
12
        };
13
14
        this.element = element;
15
        this.slides = this.element.querySelectorAll('*[data-item]');
16
17
        if (this.element.hasAttribute('data-options')) {
18
            this.options = Object.assign(this.options, JSON.parse(this.element.getAttribute('data-options')));
19
        }
20
21
        this.instance = lory(this.element, this.options);
22
23
        this.bindEvents();
24
    }
25
26
    bindEvents() {
27
        [].forEach.call(this.element.querySelectorAll('*[data-item="video"]'), (videoElement) => {
28
            let playButton = videoElement.querySelector('.slider-block__overlay--play-button');
29
30
            playButton.addEventListener('click', () => {
31
                this.currentElement = videoElement;
32
33
                if (this.currentElement.hasAttribute('data-embed')) {
34
                    utils.addClass(this.currentElement, 'has-embed');
0 ignored issues
show
Bug introduced by
The variable utils seems to be never declared. If this is a global, consider adding a /** global: utils */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
35
                    this.embedLink = this.currentElement.getAttribute('data-embed');
36
                    this.play();
37
                }
38
            });
39
        });
40
41
        this.element.addEventListener('after.lory.slide', (events) => {
42
            this.stop();
43
44
            let currentSlide = this.getCurrentSlide(events.detail.currentSlide);
45
46
            if (currentSlide && currentSlide.hasAttribute('data-autoplay') && parseInt(currentSlide.getAttribute('data-autoplay')) === 1) {
47
                this.currentElement = currentSlide;
48
49
                if (this.currentElement.hasAttribute('data-embed')) {
50
                    utils.addClass(this.currentElement, 'has-embed');
0 ignored issues
show
Bug introduced by
The variable utils seems to be never declared. If this is a global, consider adding a /** global: utils */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
51
                    this.embedLink = this.currentElement.getAttribute('data-embed');
52
                    this.play();
53
                }
54
            }
55
        });
56
    }
57
58
    getCurrentSlide(index) {
59
        let currentIndex = index - 1;
60
61
        if (currentIndex in this.slides) {
62
            return this.slides[currentIndex];
63
        }
64
65
        return false;
66
    }
67
68
    stop() {
69
        if (this.currentElement && utils.hasClass(this.currentElement, 'has-embed')) {
0 ignored issues
show
Bug introduced by
The variable utils seems to be never declared. If this is a global, consider adding a /** global: utils */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
70
            utils.removeClass(this.currentElement, 'has-embed');
71
            this.content = this.currentElement.querySelector('.slider-block__content');
72
            this.content.innerHTML = this.contentBefore;
73
            this.embedLink = null;
74
75
            this.content = null;
76
            this.currentElement = null;
77
        }
78
    }
79
80
    play() {
81
        this.content = this.currentElement.querySelector('.slider-block__content');
82
        this.contentBefore = this.content.innerHTML;
83
        this.content.innerHTML = `
84
    <div class="slider-block__content--embed">
85
        <iframe src="${this.embedLink}" width="100%" height="100%" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
86
    </div>
87
`;
88
    }
89
}
90
91
export default Slider;